package com.sl.discovery.modular.registry;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.sl.discovery.modular.DatabaseManager;
import com.sl.discovery.modular.discovery.domain.entity.AppServiceInstanceDO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;

import java.util.List;
import java.util.Properties;

import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;


@Slf4j
public class DbServiceRegistry implements ServiceRegistry<Registration> {

    private static final String STATUS_UP = "UP";

    private static final String STATUS_DOWN = "DOWN";

    private DatabaseManager databaseManager;

    public DbServiceRegistry(DatabaseManager databaseManager) {
        this.databaseManager = databaseManager;
    }

    @Override
    public void register(Registration registration) {
        if (StrUtil.isBlank(registration.getServiceId())) {
            log.warn("No service to register for nacos client...");
            return;
        }

        AppServiceInstanceDO appServiceInstanceDO = buildServiceInstance(registration);
        databaseManager.register(appServiceInstanceDO);
    }

    private AppServiceInstanceDO buildServiceInstance(Registration registration) {
        AppServiceInstanceDO appServiceInstanceDO = new AppServiceInstanceDO();
        appServiceInstanceDO.setServiceId(registration.getServiceId());
        appServiceInstanceDO.setInstanceId(registration.getInstanceId());
        appServiceInstanceDO.setHost(registration.getHost());
        appServiceInstanceDO.setPort(registration.getPort());
        appServiceInstanceDO.setSecure(BooleanUtil.isTrue(registration.isSecure()) ? 1 : 0);
        appServiceInstanceDO.setUri(registration.getUri().toString());
        appServiceInstanceDO.setMetadata(JSONUtil.toJsonStr(registration.getMetadata()));
        return appServiceInstanceDO;
    }

    @Override
    public void deregister(Registration registration) {

        log.info("De-registering from Nacos Server now...");

        if (StrUtil.isBlank(registration.getServiceId())) {
            log.warn("No dom to de-register for nacos client...");
            return;
        }
        databaseManager.deregister(registration.getInstanceId());
        log.info("De-registration finished.");
    }

    @Override
    public void close() {
        System.out.println("关闭应用了");
    }

    @Override
    public void setStatus(Registration registration, String status) {

        if (!STATUS_UP.equalsIgnoreCase(status)
                && !STATUS_DOWN.equalsIgnoreCase(status)) {
            log.warn("can't support status {},please choose UP or DOWN", status);
            return;
        }

        if (STATUS_DOWN.equalsIgnoreCase(status)) {
            databaseManager.deregister(registration.getInstanceId());
        } else {
            AppServiceInstanceDO appServiceInstanceDO = buildServiceInstance(registration);
            databaseManager.register(appServiceInstanceDO);
        }
    }

    @Override
    public Object getStatus(Registration registration) {
        AppServiceInstanceDO appServiceInstanceDO = databaseManager.selectByInstanceId(registration.getInstanceId());
        return ObjectUtil.isNotNull(appServiceInstanceDO) ? STATUS_UP : STATUS_DOWN;
    }

}
